package com.ouyy.volunteerservice.volunteer.task;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.ouyy.volunteerservice.volunteer.domain.po.VolunteerActivities;
import com.ouyy.volunteerservice.volunteer.mapper.VolunteerActivateMapper;
import com.ouyy.volunteerservice.volunteer.service.VolunteerActivateService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
@Component
public class statusApp {

    @Autowired
    private VolunteerActivateMapper activateMapper;

    @Scheduled(cron = "0 0 */3 * * ?")
    public void updateActivityStatus() {
        LocalDateTime now = LocalDateTime.now();
        System.out.println("【定时任务】更新活动状态，当前时间：" + now);

        try {
            // 1. 开始时间大于当前时间 → 设为 1（未开始）
            UpdateWrapper<VolunteerActivities> notStartedWrapper = new UpdateWrapper<>();
            notStartedWrapper
                    .gt("start_date", now)
                    .set("activity_status", 1);
            activateMapper.update(null, notStartedWrapper);

            // 2. 当前时间在 [start_date, end_date] 之间 → 设为 2（进行中）
            UpdateWrapper<VolunteerActivities> ongoingWrapper = new UpdateWrapper<>();
            ongoingWrapper
                    .le("start_date", now)
                    .ge("end_date", now)
                    .set("activity_status", 2);
            activateMapper.update(null, ongoingWrapper);

            // 3. 结束时间小于当前时间 → 设为 3（已结束）
            UpdateWrapper<VolunteerActivities> finishedWrapper = new UpdateWrapper<>();
            finishedWrapper
                    .lt("end_date", now)
                    .set("activity_status", 3);
            activateMapper.update(null, finishedWrapper);

            System.out.println("【定时任务】活动状态更新完成");

        } catch (Exception e) {
            System.err.println("【定时任务】更新失败：" + e.getMessage());
            e.printStackTrace();
        }
    }
}
